/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class BroadcomJson extends BaseJsonAdapter{

    async doRequest(){

        var options = {
            headers:{
              'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
              'Host': 'www.broadcom.com',
            },
            // uri:'http://www.bjp2p.com.cn/supervise/queryNews4List',
            uri:'https://www.broadcom.com/api/getjsonbyurl?vanityurl=company/news/product-releases&locale=avg_en&updateddate=2019-05-15-15:04:01',
            method:"GET",
            encoding:null
        }
        let body = await request(options)
        return body;
    }


    async reqJson(){
        let contents = [];
        let url = 'https://www.broadcom.com/'
        let body = await this.doRequest();
        let decodeBody = iconv.decode(body, this.encoding).toString();
        let data = JSON.parse(decodeBody);
        if ( data && data.body_highlights) {
          for(let row of data.body_highlights) {
            for(let info of row.NewsCategories) {
                for(let item of info['2019']) {
                  contents.push({
                    title: item['Title'],
                    href: url + item['_url_']
                  });
                }
            }
          }
        }


        return {data:contents}
    }
}
